function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}


$(function () {
    $(".login_form :input").focus(function () {
        $(".error_tip").hide();
    });

    $(".login_form").submit(function (e) {
        e.preventDefault();

        username = $(".login_form :input[name='username']").val();
        password = $(".login_form :input[name='password']").val();

        if (!username || !password) {
            $(".error_tip").html("请输入有效的用户名和密码").show()
            return
        }

        $(this).ajaxSubmit({
            url: "/admin/",
            type: "POST",
            headers: {
                "X-CSRFToken": getCookie('csrf_token')
            },
            success: function (resp) {
                if (resp.errno == "0") {
                    window.location.href = "/admin/index"
                }else {
                    $(".error_tip").html(resp.errmsg).show()
                }
            }
        })
    })
})